/*
 * CFile1.c
 *
 * Created: 13.02.2014 17:39:00
 *  Author: Siliconsoul
 */ 


#include <avr/io.h>
#include <stdio.h>
#include "Serial.h"

void usart_putchar(char data) {
    
    while (!(UCSR0A & (_BV(UDRE0))));
    UDR0 = data;
}

char usart_getchar(void) {
    
    while (!(UCSR0A & (_BV(RXC0))));
    return UDR0;
    
}
unsigned char usart_kbhit(void) {
    
    unsigned char b;
    b=0;
    if(UCSR0A & (1<<RXC0))
        b=1;
    return b;
    
}
void usart_pstr(char *s) {
    
    while (*s) {
        usart_putchar(*s);
        s++;
    }
    
}

int usart_putchar_printf(char var, FILE *stream) {
    
    if (var == '\n')
    usart_putchar('\r');
    usart_putchar(var);
    return 0;
    
}

static FILE mystdout = FDEV_SETUP_STREAM(usart_putchar_printf, NULL, _FDEV_SETUP_WRITE);

void usart_init() {
    
    stdout = &mystdout;
    UBRR0H = (uint8_t) (MYUBRR>>8);
    UBRR0L = (uint8_t) (MYUBRR);
    UCSR0B = (1<<RXEN0) | (1<<TXEN0);
    UCSR0C = (3<<UCSZ00);
    
}

